<%--
  Created by IntelliJ IDEA.
  User: Markrui
  Date: 2016/3/12
  Time: 21:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="header.jsp" %>
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">用户申请管理</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-sm-12">
            <br>
            <table id="user_apply_table" class="table table-hover table-bordered table-striped table-striped">
                <thead>
                <tr>
                    <td>序号</td>
                    <td>申请人</td>
                    <td>申请角色</td>
                    <td>状态</td>
                    <td>操作</td>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
    <!-- /.row -->
</div>

<%--modal--%>
<div id="user_apply_modal" class="modal fade">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">申请认证</h4>
            </div>
            <div class="modal-body">
                <div class="row">

                    <div class="form-group">
                        <form id="user_apply_form" action='' method="post">
                            <div class="col-sm-12">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-3">
                                    <label class="control-label">申请人：</label>
                                </div>
                                <div class="col-sm-6">
                                    <label id="username" class="control-label"></label>
                                </div>
                            </div>
                            <div class="col-sm-12">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-3">
                                    <label class="control-label">申请职位：</label>
                                </div>
                                <div class="col-sm-6">
                                    <label id="name" class="control-label"></label>
                                </div>
                            </div>
                            <div id="groupForms"></div>
                            <div class="col-sm-12">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-3">
                                    <label class="control-label">证书日期：</label>
                                </div>
                                <div class="col-sm-6">
                                    <label id="createtime" class="control-label"></label>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="check(1)">审核通过</button>
                <button class="btn btn-default" onclick="check(2)">审核不通过</button>
            </div>
        </div>
    </div>
</div>
<!-- /#page-wrapper -->
<%@include file="footer.jsp" %>
<script>
    $(document).ready(function() {
        loadTable();
    });
    var table;
    function loadTable(){
        $.ajax({
            url: rootUrl+'mng/apply',
            type: 'get',
            dataType: 'json'
        })
        .success(function(data){
            table = $('#user_apply_table').DataTable({
                "bProcessing": true,
                "bDestroy": true,
                "sPaginationType" : "full_numbers",
                "oLanguage" : {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "sZeroRecords": "没有检索到数据",
                    "sSearch": "搜索: &nbsp",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    }
                },
                "data":data.data.applies,
                "columns": [
                    { "data": 'userGroupApplyId' },
                    { "data": 'userEntity' },
                    { "data": 'groupEntity' },
                    { "data": 'status' },
                    { "data": 'userGroupApplyId' }
                ],
                "columnDefs": [
                    {
                        "targets": 0,
                        "render": function (data, type, row, table) {
                            return table.row + 1;
                        }
                    },
                    {
                        "targets": 1,
                        "render": function (data, type, row, table) {
                            return data.username;
                        }
                    },
                    {
                        "targets": 2,
                        "render": function (data, type, row, table) {
                            return data.name;
                        }
                    },
                    {
                        "targets": 3,
                        "render": function (data, type, row, table) {
                            var status = '审核中';
                            switch(data){
                                case 0:
                                    status = '审核中';
                                    break;
                                case 1:
                                    status = '审核通过';
                                    break;
                                case 2:
                                    status = '审核不通过';
                                    break;
                            }
                            return status;
                        }
                    },
                    {
                        "targets": -1,
                        "render": function (data, type, row, table) {
                            return '<button class="btn btn-primary" onclick="view(\''+data+'\')">查看</button>';
                        }
                    }]
            });

        });
    }

    function findTypeByGroupFormId(groupForms,groupFormId){
        if(groupForms!=null && groupForms.length > 0){
            for(var i = 0;i < groupForms.length;i++){
                if(groupForms[i].groupFormId == groupFormId){
                    return groupForms[i].type;
                }
            }
        }
    }

    var userGroupApplyId;
    function view(applyId){
        userGroupApplyId = applyId;
        $.ajax({
            url:rootUrl+ 'mng/apply/'+applyId,
            type: 'get',
            dataType: 'json'
        })
        .success(function(data){
            console.log(data);
            var apply = data.data.apply;
            $("#username").html(apply.userEntity.username);
            $("#name").html(apply.groupEntity.name);
            var html = "";
            for (var i = 0; i < apply.userGroupFormValues.length; i++){

                var value = "";

                console.log("表单信息是：",apply.userGroupFormValues[i]);
                if(findTypeByGroupFormId(apply.groupEntity.groupForms,apply.userGroupFormValues[i].groupFormId)==2){
                    console.log("是文件",findTypeByGroupFormId(apply.groupEntity.groupForms,apply.userGroupFormValues[i].groupFormId));
                    value = '<a class="btn" target="_blank" href="'+apply.userGroupFormValues[i].value+'">查看或下载</a>';
                }else{
                    value = apply.userGroupFormValues[i].value;
                    console.log("不是文件",findTypeByGroupFormId(apply.groupEntity.groupForms,apply.userGroupFormValues[i].groupFormId));
                }

                html += '<div class="col-sm-12">'+
                        '<div class="col-sm-1"></div>'+
                        '<div class="col-sm-3">'+
                        '<label class="control-label">'+apply.groupEntity.groupForms[i].label+'</label>'+
                        '</div>'+
                        '<div class="col-sm-6">'+
                        '<label class="control-label">'+value+
                        '</label>'+
                        '</div>'+
                        '</div>';
            }
            $("#groupForms").html(html);
            $("#createtime").html((new Date(apply.createtime)).Format("yyyy-MM-dd hh:mm:ss"));
        });
        $('#user_apply_modal').modal({
            backdrop : "static"
        });
    }

    function check(result){
        $.ajax({
            url:rootUrl+ 'mng/apply/'+userGroupApplyId+'/state/'+result,
            type: 'put',
            dataType: 'json'
        })
        .success(function(data){
            if(data.code == 200){
                $('#user_apply_modal').modal('hide');
            }
            location.reload();
        });
    }
</script>